package com.zzjson.calendar;

import com.zzjson.job.MyJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.AnnualCalendar;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.calendar</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年12月11日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class CalendarDemo {
	public static void main(String[] args) throws SchedulerException {
		StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = stdSchedulerFactory.getScheduler();
		scheduler.start();

		//定义日历-假期
		AnnualCalendar holidays = new AnnualCalendar();

		//排除日期
		GregorianCalendar myDay = new GregorianCalendar(2019, 8, 8);
		holidays.setDayExcluded(myDay, true);

		//排除中秋节
		GregorianCalendar midAutumn = new GregorianCalendar(2019, 9, 13);
		holidays.setDayExcluded(midAutumn, true);

		//	排除圣诞节
		GregorianCalendar christmas = new GregorianCalendar(2019, 12, 25);
		holidays.setDayExcluded(christmas, true);

		scheduler.addCalendar("holidays", holidays, false, false);


		JobDetail jobDetail = JobBuilder.newJob(MyJob.class).
				withIdentity("job1", "group1").
				usingJobData("name", "zzy").build();

		SimpleTrigger trigger = TriggerBuilder.newTrigger().
				withIdentity("trigger1", "group1").
				startNow().
				withSchedule(
						SimpleScheduleBuilder.simpleSchedule().
								withIntervalInHours(2).
								repeatForever()).
				build();

		Date firstRunTime = scheduler.scheduleJob(jobDetail, trigger);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		System.out.println(jobDetail.getKey() + "第一次触发: " + simpleDateFormat.format(firstRunTime));

	}
}